package com.bfly.mall.advert.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.Login;
import com.bfly.mall.advert.entity.Coupon;
import com.bfly.mall.advert.dto.CouponListDTO;
import com.bfly.mall.advert.entity.CouponRangeBind;
import com.bfly.mall.advert.dto.CouponGetRecordListDTO;
import com.bfly.mall.advert.enums.CouponStatus;
import com.bfly.mall.advert.enums.CouponUseRange;
import com.bfly.mall.advert.enums.CouponUseStatus;
import com.bfly.mall.advert.service.ICouponService;
import com.bfly.mall.advert.service.ICouponGetRecordService;
import com.bfly.mall.product.entity.Product;
import com.bfly.mall.product.service.IProductService;
import com.bfly.mall.user.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 优惠卷API
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 22:48
 */
@RestController
@RequestMapping(value = "/api/coupon")
public class CouponApiController extends BaseController {

    @Autowired
    private ICouponService couponService;
    @Autowired
    private ICouponGetRecordService couponGetRecordService;
    @Autowired
    private IProductService productService;

    /**
     * 有效可领取的优惠卷
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/18 22:48
     */
    @GetMapping(value = "/available")
    public void listAvailableCoupon(HttpServletResponse response, @RequestParam(value = "productId", defaultValue = "0") int productId) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("status", CouponStatus.ON_SHELF.getId());
        List<Coupon> coupons = couponService.getList(param);
        List<CouponListDTO> list = new ArrayList<>();
        if (coupons != null) {
            if (productId != 0) {
                Product product = productService.get(productId);
                if (product != null) {
                    // 如果指定了产品 则获取与产品相关的可用的优惠卷
                    coupons.stream().filter(coupon -> {
                        CouponUseRange range = CouponUseRange.getRange(coupon.getUseRange());
                        if (range == CouponUseRange.ALL) {
                            // 全场通用卷
                            return true;
                        }
                        List<CouponRangeBind> couponRangeBinds = coupon.getCouponRangeBinds();
                        boolean exist = false;
                        if (couponRangeBinds != null) {
                            for (CouponRangeBind couponRangeBind : couponRangeBinds) {
                                // 指定产品ID和类型
                                if ((productId == couponRangeBind.getRangeId() && range == CouponUseRange.PRODUCT) ||
                                        (product.getCategoryId() == couponRangeBind.getRangeId() && range == CouponUseRange.CATEGORY)) {
                                    exist = true;
                                    break;
                                }
                            }
                        }
                        return exist;
                    });
                }
            }
            for (Coupon coupon : coupons) {
                CouponListDTO dto = new CouponListDTO();
                BeanUtils.copyProperties(coupon, dto);
                list.add(dto);
            }
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 领取优惠卷
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/13 12:01
     */
    @GetMapping(value = "/receive")
    @Login
    public void receiveCoupon(HttpServletResponse response, @RequestParam("couponId") int couponId) {
        User user = getUser();
        couponGetRecordService.receiveCoupon(user.getId(), couponId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 加载用户领取的优惠卷
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/16 19:52
     */
    @GetMapping(value = "/list")
    @Login
    public void getUserCoupons(HttpServletResponse response, @RequestParam(value = "status",defaultValue = "0")int status) {
        CouponUseStatus useStatus=CouponUseStatus.getStatus(status);
        List<CouponGetRecordListDTO> coupons = couponGetRecordService.getUserCoupon(getUser().getId(), useStatus);

        Iterator<CouponGetRecordListDTO> it = coupons.iterator();
        while (it.hasNext()) {
            CouponGetRecordListDTO dto = it.next();
            dto.setCouponRangeBinds(null); //设置为null 不需要序列化到前端
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(coupons));
    }
}